const app = getApp()
var http = require('../../utils/http')
var utils = require('../../utils/util')
Page({

    data: {
        //分页信息
		queryInfo: {
			//页码
			pageNumber: 1,
			//页数
			pageSize: 9,
			//搜索字段
			queryString: ''
        },
        //表格数据
        tableList: [],
        expertList:[],
		//首页展示图片
        imageURL: '',
        showModalStatus: false,
        animationData:{},
        lookLikeFavourite:{},
        navHeight: '',
        menuButtonInfo: {},
        searchMarginTop: 0, // 搜索框上边距
        searchWidth: 0, // 搜索框宽度
        searchHeight: 0, // 搜索框高度
    },

    
    onLoad(options) {
        this.setData({
            menuButtonInfo: wx.getMenuButtonBoundingClientRect()
          })
        //   console.log(this.data.menuButtonInfo)
          const { top, width, height, right } = this.data.menuButtonInfo
          wx.getSystemInfo({
            success: (res) => {
              const { statusBarHeight } = res
              const margin = top - statusBarHeight
              this.setData({
                navHeight: (height + statusBarHeight + (margin * 2)),
                searchMarginTop: statusBarHeight + margin, // 状态栏 + 胶囊按钮边距
                searchHeight: height,  // 与胶囊按钮同高
                searchWidth: right - width // 胶囊按钮右边坐标 - 胶囊按钮宽度 = 按钮左边可使用宽度
              })
            },
          })
        this.findPage('加载中....');
        this.findExpertList();
    },
    bindblur(e){
        this.setData({
            'queryInfo.pageNumber':1,
            'queryInfo.queryString':e.detail.value
        })
        this.findPage("数据加载中。。。")
        
    },
    //查找专家列表
    findExpertList(){
        http.post("/user/findExpert",this.data.queryInfo,res=>{
            this.setData({
                expertList:res.data.rows
            })
        })
    },
    toExpertDetails(events) {
        let item = events.currentTarget.dataset.item;
        item = encodeURIComponent(JSON.stringify(item)) 
        wx.navigateTo({
            url: '/pages/expertDetails/expertDetails?item='+item
        })
    },
    powerDrawer: function (e) {
        http.get("/mini/testToken","",res=>{
            if(res.statusCode == 401){
                wx.navigateTo({
                    url: '/pages/login1/login1'
                })
            }else{
                var currentStatu = e.currentTarget.dataset.statu;
                this.util(currentStatu)
            }
        })
		
    },
    // 获取分页数据，massage是提示语
	findPage(message) {
		//加载提示
		wx.showNavigationBarLoading();
		wx.showToast({
			title: message,
			icon: 'loading',
			duration: 1000
        })
        const that = this;
        const token = wx.getStorageSync('token')
        if(token != ''){
            http.post("/moments/findPage",this.data.queryInfo,res=>{
                if (res.statusCode == 401) {
                    wx.clearStorageSync()
                    that.findPage("数据加载中。。。")
                }else{
                    //将原有的结果列表定义为临时数组
                    let temp = this.data.tableList;
                    //将新的结果接收
                    let result1 = res.data.rows;
                    let result = result1.map((item, index) => {
                        //时间格式化
                        item.createTime = utils.formatTime(item);
                        const content = item.content
                        if (item.title == null) {
                            item.content = utils.toHtml(item.content)
                        }else{
                            item.content = utils.toMarkdown(item.content)
                        }
                        //从文章中提取图片链接
                        return Object.assign(item, {
                            'imageURL': utils.get_url(content)
                        })
                    })
                    utils.pageHelper(result,temp,this)
                }
            })
        }else{
            http.postNoHeader("/moments/findPage",this.data.queryInfo,res=>{
                //将原有的结果列表定义为临时数组
                let temp = this.data.tableList;
                //将新的结果接收
                let result1 = res.data.rows;
                let result = result1.map((item, index) => {
                    //时间格式化
                    item.createTime = utils.formatTime(item);
                    const content = item.content
                    if (item.title == null) {
                        item.content = utils.toHtml(item.content)
                    }else{
                        item.content = utils.toMarkdown(item.content)
                    }
                    //从文章中提取图片链接
                    return Object.assign(item, {
                        'imageURL': utils.get_url(content)
                    })
                })
                utils.pageHelper(result,temp,this)
            })
        }
    },
    
	onPullDownRefresh: function () {
		this.data.queryInfo.pageNumber = 1;
		this.findPage('数据刷新中....');
	},
	
	onReachBottom: function () {
		this.findPage('数据加载中....')
		
    },
    
    //跳转到专家列表
    toExpertList() {
        wx.navigateTo({
            url: '/pages/expertList/expertList'
        })
    },
    //跳转到编辑文章
    toaddArticle(event) {
        let type = event.currentTarget.dataset.type;
		this.util('close')
        wx.navigateTo({
            url: '/pages/moment/addArticle/addArticle?type='+type
		})
    },
    //动画
    util: function (currentStatu) {
        /* 动画部分 */
        // 第1步：创建动画实例
        var animation = wx.createAnimation({
            duration: 300, //动画时长
            timingFunction: "linear", //线性
            delay: 0 //0则不延迟
        });
        // 第2步：这个动画实例赋给当前的动画实例
        this.animation = animation;
        if (currentStatu=="open") {
            // 第3步：执行第一组动画
            animation.opacity(1).translateY(-200).step();
            this.setData({
                showModalStatus: true
            })
        }else{
            animation.opacity(0).translateY(0).step();
            this.setData({
                showModalStatus: false
            })
        }
        // 第4步：导出动画对象赋给数据对象储存
        this.setData({
            animationData: animation.export()
        })

        // 显示
        if (currentStatu == "open") {
            this.setData({
                showModalStatus: true
            });
        }
    },
    
    onShow: function () {
        this.getTabBar().init();
        this.onSoekctCallback();
    },
    // 收到消息处理
    onSoekctCallback: function () {
        app.globalData.callback = (res) => {
            console.log("获取到消息了", res.data);
            if (res.data === 'success' || res.data === 'heartBath') return
        }
    },



})